/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 * 
 * @Author: ipk518 121206530@qq.com
 * @Date: 2025-06-28 10:00:14
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-06-28 10:26:07
 * @FilePath: /etws-sxk2307/tools/testTools/txtTools.c
 * @Description: 
 * 你把时间投在专业上，两三年，你就能在圈子里小有名气。四五年，你就能靠这个专业赚钱。过了10年以上，你就能成为这个领域的专家
 * Copyright (c) 2025 by etws@quyujiang, All Rights Reserved. 
 */
#include <stdio.h>
#include "time.h"
#include <stdlib.h>
#include <stdint.h>
#include "string.h"

static void servo(int h,int v)
{

#define SERVO_DIFF_PATH "text.txt"

    static uint16_t m_prevVOperaLoc = 0, m_prevHOperaLoc = 0;//TODO:2025-6-11
    uint16_t m_diffV, m_diffH;

    m_diffH = abs(h - m_prevHOperaLoc);
     if (m_diffH > 18000) { // 若差值超过 180 度（18000 * 0.01）
        m_diffH = 36000 - m_diffH; // 取互补差值
    }
    m_diffV = abs(v - m_prevVOperaLoc);
    if (m_diffV > 18000) { // 若差值超过 180 度（18000 * 0.01）
        m_diffV = 36000 - m_diffV; // 取互补差值
    }
    
    if (m_diffV > 20) { //0.2
        printf("俯仰方向角度变化异常,俯仰角度:上一次=%d,本次=%d.\r\n", m_prevVOperaLoc, v);
        // 新增日志保存到文件
         FILE* fp = fopen(SERVO_DIFF_PATH, "a");
         if(fp) {
         	time_t now = time(NULL);
         	struct tm *tm_now = localtime(&now);
         	fprintf(fp, "[%04d-%02d-%02d %02d:%02d:%02d][ERROR] 俯仰方向角度变化异常(0.01): 上一次=%d, 本次=%d\n", 
         			tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday,
         			tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec,
         			m_prevVOperaLoc, v);
         	fclose(fp);
         }
    }
    if (m_diffH > 300) { //3
        printf("水平方向角度变化异常,水平角度:上一次=%d,本次=%d.\r\n", m_prevHOperaLoc, h);
        // 新增日志保存到文件
         FILE* fp = fopen(SERVO_DIFF_PATH, "a");
         if(fp) {
         	time_t now = time(NULL);
         	struct tm *tm_now = localtime(&now);
         	fprintf(fp, "[%04d-%02d-%02d %02d:%02d:%02d][ERROR] 水平方向角度变化异常(0.01): 上一次=%d, 本次=%d\n", 
         			tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday,
         			tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec,
         			m_prevHOperaLoc, h);
         	fclose(fp);
         }
    }
    m_prevVOperaLoc = v;
    m_prevHOperaLoc = h;
}

int main()
{   
    int hh = 0, vv = 0;
    while (1)
    {
        for (hh = 0, vv = 0; hh < 36000; hh++, vv++)
        {
            servo(hh, vv);
        }
        hh = 0, vv = 0;
    }
    
}